/**
 * 单列模式（singleton）
 */

class Singleton {

    public static void main(String[] args) {
        Person person01 = LazyInstance.getPerson();
        Person person02 = LazyInstance.getPerson();

        if (person01 == person02) {
            System.out.println("ok");
        }
        else {
            System.out.println("failure");
        }
    }


}

/**
 * 饿汉模式
 */
class HungerInstance{
    private final static Person person = new Person("i will stand firm ...");

    private HungerInstance() {
    }

    public static Person getPerson() {
        return person;
    }
}

/**
 * 懒汉模式
 */
class LazyInstance{
    private static Person person;

    private LazyInstance() {
    }

    public static synchronized Person getPerson() {
        if (person == null) {
            person = new Person("i will stand firm!");
        }
        return person;
    }
}


class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}